package com.legsem.jhosync.dto;

import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.map.annotate.JsonSerialize;

import com.legsem.jhosync.SourceConfig;
import com.legsem.jhosync.face.PartitionType;
import com.legsem.jhosync.model.Client;

/**
 * Encapsulates the data a client needs to know when it was just created on the
 * server side.
 * 
 */
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ClientCreate {

    private Map < String, Map < String, Object >> sources;
    private Map < String, Object > client;

    public ClientCreate() {

    }

    public ClientCreate(Client client,
            Map < String, SourceConfig > sourceConfigs) {
        if (client != null) {
            this.client = new HashMap < String, Object >();
            this.client.put("client_id", client.getId());
        }

        sources = new HashMap < String, Map < String, Object >>();
        for (SourceConfig sourceConfig : sourceConfigs.values()) {
            Map < String, Object > attr = new HashMap < String, Object >();
            attr.put("poll_interval", sourceConfig.getPollInterval());
            if (sourceConfig.getPartitionType() == PartitionType.APP) {
                attr.put("partition_type", PartitionType.APP.toString()
                        .toLowerCase());
            }
            sources.put(sourceConfig.getName(), attr);
        }
    }

    public Map < String, Map < String, Object >> getSources() {
        return sources;
    }

    public void setSources(Map < String, Map < String, Object >> sources) {
        this.sources = sources;
    }

    public Map < String, Object > getClient() {
        return client;
    }

    public void setClient(Map < String, Object > client) {
        this.client = client;
    }

    @Override
    public String toString() {
        return "ClientCreate [sources=" + sources + ", client=" + client + "]";
    }

}
